package com.jubotech.business.web.service;

import com.jubotech.business.web.dao.SysAutoSettingDao;
import com.jubotech.business.web.dao.SysConfigDao;
import com.jubotech.business.web.domain.SysAutoSetting;
import com.jubotech.business.web.domain.SysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional // 支持事务
@CacheConfig(cacheNames="sysConfig")
public class SysConfigService {

    @Autowired
    private SysConfigDao sysConfigDao;


    @Cacheable(key = "#config")
    public SysConfig getByConfig(String config) {
        return sysConfigDao.getByConfig(config);
    }

    @CacheEvict(/*value="system",*/allEntries=true)
    public int update(SysConfig info) {
        return sysConfigDao.updateById(info);
    }

    public SysConfig insert(SysConfig info) {
        try {
            SysConfig sys = sysConfigDao.getByConfig(info.getConfig());
            if (null == sys) {
                sysConfigDao.insert(info);
            } else {
                sysConfigDao.delete(sys);
                sysConfigDao.insert(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
            info = null;
        }
        return info;
    }

    @CacheEvict(/*value="system",*/allEntries=true)
    public int delete(SysConfig info) {
       return sysConfigDao.delete(info);
    }
}
